package cn.edu.zafu.bigdata.Wym;

import java.sql.*;
import java.util.Scanner;

public class Wym_StudentManagementSystem1 {

    private static final String DB_URL = "jdbc:mysql://localhost:3306/jdbc";
    private static final String USER = "root";
    private static final String PASS = "440371874Ym@";

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("学生信息管理系统");
            System.out.println("1. 添加学生信息");
            System.out.println("2. 检索学生信息");
            System.out.println("3. 更新学生信息");
            System.out.println("4. 删除学生信息");
            System.out.println("5. 退出程序");
            System.out.print("请输入操作编号 (1-5): ");
            int choice = scanner.nextInt();
            scanner.nextLine(); // 消耗换行符

            switch (choice) {
                case 1:
                    addStudent(scanner);
                    break;
                case 2:
                    retrieveStudent(scanner);
                    break;
                case 3:
                    updateStudent(scanner);
                    break;
                case 4:
                    deleteStudent(scanner);
                    break;
                case 5:
                    System.out.println("退出程序。");
                    return;
                default:
                    System.out.println("无效的输入，请重新输入。");
            }
        }
    }

    private static void addStudent(Scanner scanner) {
        System.out.print("请输入学号: ");
        String id = scanner.nextLine();
        System.out.print("请输入姓名: ");
        String name = scanner.nextLine();
        System.out.print("请输入电话: ");
        String phone = scanner.nextLine();

        try (Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);
             PreparedStatement pstmt = conn.prepareStatement("INSERT INTO students (id, name, phone) VALUES (?, ?, ?)")) {
            pstmt.setString(1, id);
            pstmt.setString(2, name);
            pstmt.setString(3, phone);
            int result = pstmt.executeUpdate();
            if (result > 0) {
                System.out.println("学生信息添加成功。");
            } else {
                System.out.println("学生信息添加失败。");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    private static void retrieveStudent(Scanner scanner) {
        System.out.print("请输入要检索的学号: ");
        String id = scanner.nextLine();

        try (Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);
             PreparedStatement pstmt = conn.prepareStatement("SELECT * FROM students WHERE id = ?")) {
            pstmt.setString(1, id);
            ResultSet rs = pstmt.executeQuery();
            if (rs.next()) {
                System.out.println("学号: " + rs.getString("id"));
                System.out.println("姓名: " + rs.getString("name"));
                System.out.println("电话: " + rs.getString("phone"));
            } else {
                System.out.println("未找到该学生。");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    private static void updateStudent(Scanner scanner) {
        System.out.print("请输入要更新的学号: ");
        String id = scanner.nextLine();
        System.out.print("请输入新的姓名: ");
        String name = scanner.nextLine();
        System.out.print("请输入新的电话: ");
        String phone = scanner.nextLine();

        try (Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);
             PreparedStatement pstmt = conn.prepareStatement("UPDATE students SET name = ?, phone = ? WHERE id = ?")) {
            pstmt.setString(1, name);
            pstmt.setString(2, phone);
            pstmt.setString(3, id);
            int result = pstmt.executeUpdate();
            if (result > 0) {
                System.out.println("学生信息更新成功。");
            } else {
                System.out.println("学生信息更新失败。");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    private static void deleteStudent(Scanner scanner) {
        System.out.print("请输入要删除的学号: ");
        String id = scanner.nextLine();

        try (Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);
             PreparedStatement pstmt = conn.prepareStatement("DELETE FROM students WHERE id = ?")) {

            pstmt.setString(1, id);
            int result = pstmt.executeUpdate();

            if (result > 0) {
                System.out.println("学生信息删除成功。");
            } else {
                System.out.println("未找到该学生或删除失败。");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}